package com.nono.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nono.Dao.AdminDao;

@Controller
public class MainController {
	@Autowired
	AdminDao adminDao;
	
	//获取所有的商品类型
	@RequestMapping(value="getTypes", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getTypes() {
		JSONArray jsonArray = JSONArray.fromObject( adminDao.getTypes() );
		return jsonArray;
	}

	//获取所有的商品
	@RequestMapping(value="getAllCom", method=RequestMethod.POST)
	@ResponseBody
	public JSONObject getAllCom(HttpServletRequest request, HttpServletResponse response) {
		return JSONObject.fromObject( adminDao.getAllCom( ) );
	}

	//获取商品by他的类型;
	@RequestMapping(value="getComById", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getComById(HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.parseInt( request.getParameter("id") );
		return JSONArray.fromObject( adminDao.getComById( id ) );
	}
	
	//获取商品by他的类型;
	@RequestMapping(value="getComByType", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getComByType(HttpServletRequest request, HttpServletResponse response) {
		String type = request.getParameter("type");
		return JSONArray.fromObject( adminDao.getComByType( type ) );
	}
	
	//搜索产品;
	@RequestMapping(value="search", method=RequestMethod.POST) 
	@ResponseBody
	public JSONArray search(HttpServletRequest request, HttpServletResponse response){
		return JSONArray.fromObject( adminDao.search(request.getParameter("keyword")) );
	}
}
